// pages/result/result.js
var app = getApp(); //获取应用实例
const quote = app.globalData.quote;

const recorder = app.globalData.recorder; //引入录音对象
const innerAudioContext = app.globalData.innerAudioContext; //引入音频播放器

const tools = quote.tools;
const mta = quote.mta;
const api = quote.api;
const util = quote.util;
const path = quote.paths;


Page({
    /**
     * 页面的初始数据
     */
    data: {
        path: path,
        VID: null, //练习语句id
        recordFilePath: null, //录音文件路径
        result: null, //录音结果
        record_scale: 100, //分制
        difficultWords: null, //难点词汇数组
        isPlaying: false, //是否正在播放
        voiceDuration: null, //用户录音时长
        playTimeText: null, //用户录音时长事件格式
        remain: 0, //播放录音剩余时间
        playtimeKeeping: 0, //播放录音计时
        rankNum: '', //用户排名
        TopScore: '', //最高得分
    },

    onLoad(options) {
        console.log('传入的数据', options)
        let result = JSON.parse(options.result);
        this.setData({
            recordFilePath: options.filePath,
            result: result,
            VID: options.VID,
            voiceDuration: options.voiceDuration,
            playTimeText: tools.timeFormat(options.voiceDuration),
            TopScore: options.TopScore,
            rankNum: options.rankNum
        })
        this.difficultWords();
    },

    //获取难点词汇
    difficultWords() {
        let _self = this;
        let dataDic = {
            VID: _self.data.VID,
            //VID: 35697
        };
        api.methodGetDifficultWords({
            dataDic,
            success: (res) => {
                console.log('难点词汇数据', res) //res.data.data.DifficultWords
                var DifficultWords = res.data.data.DifficultWords;
                _self.setData({
                        difficultWords: DifficultWords.length > 0 ? DifficultWords : []
                    })
                    // [{
                    //     "Word": "endeavor",
                    //     "WordClass": "vt.& vi.尝试，试图;尽力，竭力 | n.努力，尽力",
                    //     "WordUse": "endeavor to 尝试做某事",
                    //     "WordRoot": "",
                    //     "WordDerivative": "endeavour （n. 尽力，竭力）"
                    // }]
            },
            fail: (res) => {
                console.log(res);
            },
            complete: (res) => {
                console.log(res.data);
            },
        })
    },
    //播放用户录音
    playRecord() {
        console.log("播放用户录音")
        let _self = this;
        if (_self.data.isPlaying) {
            console.log("正在播放录音，请稍后再点")
            return
        }
        clearInterval(timer_play);
        let timer_play = setInterval(function() {
            _self.setData({
                playtimeKeeping: _self.data.playtimeKeeping + 1,
                playTimeText: tools.timeFormat(_self.data.playtimeKeeping + 1)
            });
            console.log('计时', _self.data.playtimeKeeping);
            //当剩余时间为零时，停止播放录音
            if (_self.data.playtimeKeeping == _self.data.voiceDuration) {
                _self.setData({
                    isPlaying: false,
                })
                clearInterval(timer_play);
            }
        }, 1000);
        _self.timer_play = timer_play
        _self.setData({
            isPlaying: true,
            playtimeKeeping: 0,
            playTimeText: "00:00"
        })
        innerAudioContext.src = _self.data.recordFilePath;
        innerAudioContext.play();

        innerAudioContext.onEnded((res) => {
            console.log('正常停止播放', res)
        })
    },
    //停止播放用户录音
    stopRecord() {
        console.log("停止用户录音")
        let _self = this;
        clearInterval(_self.timer_play);
        innerAudioContext.stop();
        _self.setData({
            isPlaying: false,
            playTimeText: tools.timeFormat(_self.data.voiceDuration)
        })
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
})